package com.sgb.goods.view.activity.details.order;


import android.text.TextUtils;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ColorUtils;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityLogisticsAgreementBinding;
import com.sgb.goods.entity.order.LogisticsAgreementEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.SpannableUtils;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.LogisticsAgreementModel;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 描述：商品订单中的物流约定
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.16
 */
@Route(path = Constants.ARouter.AR_LOGISTICS_AGREEMENT)
public class LogisticsAgreementActivity extends BaseActivity<GoodsActivityLogisticsAgreementBinding, LogisticsAgreementModel>
        implements SmartRefreshLayoutHelper {

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_logistics_agreement;
    }

    @Override
    protected void initListener() {
        mDataBinding.headContainer.ivBack.setOnClickListener(v -> finish());
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(LogisticsAgreementModel.class, BR.viewModel);
    }

    @Override
    protected void initObserve() {
        mViewModel.logisticsAgreementEntities.observe(LogisticsAgreementActivity.this, logisticsAgreementEntities -> {
            if (null == logisticsAgreementEntities || logisticsAgreementEntities.size() == 0) {
                mViewModel.showNoData(mDataBinding.goodsViewContainer);
            } else {
                showPrice(logisticsAgreementEntities);
            }
        });
    }

    @Override
    protected void init() {
        initView();
        loadData();
    }

    private void initView() {
        mDataBinding.headContainer.title.setText("物流约定");
        DividerItemDecoration divider = new DividerItemDecoration(LogisticsAgreementActivity.this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(LogisticsAgreementActivity.this, R.drawable.goods_divider_item_decoration_5)));
        mDataBinding.goodsRlvView1.addItemDecoration(divider);
    }

    private void loadData() {
        String orderId = getIntent().getStringExtra("orderId");
        mViewModel.getLogisticsAgreement(orderId);
    }

    /**
     * 计算并显示配送费合计
     */
    private void showPrice(List<LogisticsAgreementEntity> logisticsAgreementEntities) {
        BigDecimal decimal = new BigDecimal("0");
        for (LogisticsAgreementEntity entity : logisticsAgreementEntities) {
            if (!TextUtils.isEmpty(entity.money)) {
                decimal = decimal.add(new BigDecimal(entity.money));
            }
        }
        // 设置文字样式
        mDataBinding.goodsTotalPrice.setText(SpannableUtils.getMoneyStyle1(
                "配送费合计  ",
                13,
                ColorUtils.getColor(R.color.goods_color_FF333333),
                "￥", decimal.toString()));
    }
}